package com.pptree.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.pptree.service.UserService;
import com.pptree.service.impl.UserServiceImpl;
import com.pptree.vo.User;

@WebServlet("/signin")
public class SignInServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
	private UserService userService = new UserServiceImpl();
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		String username = request.getParameter("username"); //name="usernmae"
		String password = request.getParameter("password");//name="password"
		
		User user = userService.login(username, password);
		if(user == null){
			request.setAttribute("errorMsg", "登录名或密码错误,请重新输入！");
			request.getRequestDispatcher("/index.jsp").forward(request, response);
		}
		else{
			HttpSession session = request.getSession();
			session.setAttribute("user", user);
			if(user.getUserType() == 0){
				response.sendRedirect("admin/manager.jsp");
			}
			else
			{
				response.sendRedirect("index.jsp");
			}
		}
		
	}

}
